package com.crm.customers.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.customers.dao.CustomerContactsDao;
import com.crm.customers.dao.CustomerDao;
import com.crm.customers.model.CustomerContacts;
import com.crm.customers.model.CustomerInfo;
import com.crm.customers.service.CustomerContactsService;

@Service("customerContactsService")
public class CustomerContactsServiceImpl implements CustomerContactsService {

	@Autowired
	private CustomerContactsDao customerContactsDao;
	@Autowired
	private CustomerDao customerDao;
	
	@Override
	public CustomerContacts insertCustomerContacts(CustomerContacts ccontacts) {
		return  customerContactsDao.insertCustomerContacts(ccontacts);
	}

	@Override
	public CustomerContacts updateCustomerContacts(CustomerContacts ccontacts) {
		return  customerContactsDao.updateCustomerContacts(ccontacts);
	}

	@Override
	public List<CustomerContacts> getAllCustomerContactsId(Long id) {
		return customerContactsDao.getCustomerContactsList(id);
	}

	@Override
	public CustomerContacts getCustomerContactsById(Long id) {
		return customerContactsDao.getCustomerContactsByid(id);
	}

	@Override
	public int gettelcount(CustomerContacts ccontacts) {
		return customerContactsDao.getTelCount(ccontacts);
	}

	@Override
	public int saveMainContacts(CustomerContacts ccontacts) {
		int flag = 0;
		CustomerContacts  result =   getCustomerContactsById(ccontacts.getId());
		if(result!=null&&result.getId()!=null){
			//更新原主联系人为空 
			customerContactsDao.updateCustomerMainContacts(ccontacts);
			ccontacts.setIsmain(1);
			//设置主联系人
			updateCustomerContacts(ccontacts);
			//更新主要联系人电话到基本信息
			CustomerInfo cinfo = new CustomerInfo();
			cinfo.setId(ccontacts.getCusid());
			cinfo.setTel(result.getPhone());
			customerDao.updateCustomerInfo(cinfo);
			flag=1;
		} 
		return flag;
	}

	@Override
	public CustomerContacts getcustconlistByphone(CustomerContacts ccontacts) {
		return customerContactsDao.getcustconlistByphone(ccontacts);
	}

}
